<?php
namespace app\index\model;
class User extends BaseModel{
    
    
    function getUserByOpenId($openid){
        $user = D('user');
        return $user->where("openid",$openid)->find();
    }
    function getMyMoney($id){
        $user = D('user');
        $res = $user->where("id",$id)->find();
        return round(($res['money']/100),2);
    }
    function isinblack($id,$isopenId=false){
       
        $user = D('user');
        if ($isopenId){
            $res = $user->where("openid",$id)->find();
        }else {
            $res = $user->where("id",$id)->find();
        }
       
        if(!$res||$res['is_black']==1){
            return false;
        }
        return true;
    }
    function getUserByUserId($id){
        $user = D('user');
        return $user->where("id",$id)->find();
    }
    function getUserByUserId2($id){
        $user = D('user');
        $res =  $user->where("id",$id)->find();
        if(!$res||$res['is_black']==1){
            return false;
        }
        return $res;
    }
    function getUserByShareId($id){
        $user = D('user');
        return $user->where("share_id",$id)->find();
    }
    function updateMoney($id,$money,$isadd=false){
        $user = D('user');
        $data['id']=$id;
        $res = $user->where("id",$id)->find();
        if ($isadd){
            $realmoney = $res['money'] + $money;
            $data['money'] = $realmoney;
        }else {
            $realmoney = $res['money'] - $money;
            $data['money'] = $realmoney;
            if ($data['money']<=0){
                $data['money']=0;
            }
        }
        $user->update($data);
        return $data['money'];
    }
    function getUserWxToken($user_id){
        $user = D('wxtoken');
        return $user->where("user_id",$user_id)->find();
    }
    function getUserWxToken2($openid){
        $res = $this->getUserByOpenId($openid);
        $user = D('wxtoken');
        return $user->where("user_id",$res['id'])->find();
    }
    function addUser($info){
        $res = $this->getUserByOpenId($info['openid']);
        $user = D('user');
        if($res){
            $info['id'] = $res['id'];
            $info['share_id'] = $res['share_id'];
            $user->update($info);
            return $info;
        }else {
            $info['share_id'] = uuidBy32();
            $id = $user->insertGetId($info);
            $info['id'] = $id;
            return $info;
        }
    }
    function addYongjin($info){
        $uyongjin = D('uyongjin');
        $uyongjin->insert($info);
    }
    function addToken($info){
        $res = $this->getUserWxToken($info['user_id']);
        $user = D('wxtoken');
        if($res){
            $info['id'] = $res['id'];
            $user->update($info);
        }else {
            $user->insert($info);
        }
    }
    
    
    function addShareUser($myuserid,$otheruserid) {
        $tuijian = D('tuijian');
        $res = $tuijian->where('from_id',$otheruserid)->find();
        if (!$res) {
            $data['user_id'] = $myuserid;
            $data['from_id'] = $otheruserid;
            $tuijian->insert($data);
        }
        
    }
    function applyDaili($userid){
        $res = $this->getUserByUserId($userid);
        $user=D('user');
        $res['is_daili'] = 1;
        $user->update($res);
    }
    function getMyTuiJian($userId){
        $tuijian = D('tuijian');
        $res = $tuijian->where('user_id',$userId)->order("create_time desc")->select();
        return $res;
    }
    function getMyTuiJianAll($userId){
        $data = [];
        $data1 = [];
        $data2 = [];
        $data3 = [];
        $res1 = $this->getMyTuiJian($userId);        
        foreach ($res1 as $tmp1){
            
            array_push($data1, $this->getMyTuiJianInfo($tmp1));
            $res2 = $this->getMyTuiJian($tmp1['from_id']);
            foreach ($res2 as $tmp2){
            array_push($data2, $this->getMyTuiJianInfo($tmp2));
                $res3 = $this->getMyTuiJian($tmp2['from_id']);               
                foreach ($res3 as $tmp3){
                    array_push($data3, $this->getMyTuiJianInfo($tmp3));
                }
            }
        }
        $data[0]=array_sort($data1, "create_time");
        $data[1]=array_sort($data2, "create_time");
        $data[2]=array_sort($data3, "create_time");
        return $data;
    }
    
    function gettuijianCount($userId){
        $data = $this->getMyTuiJianAll($userId);
        
        
        return count($data[0]) + count($data[1]) + count($data[2]);
    }
    
    function getMyTuiJianInfo($tmp){
        $userinfo = $this->getUserByUserId($tmp['from_id']);
        $tmp['nickname'] = $userinfo['nickname'];
        $tmp['imgurl'] = $userinfo['imgurl'];
        return $tmp;
    }
    function getDailiBaifenbi($isdefault,$type,$id=null){
        //$isdefault,0不适用默认，1使用默认
        $dyongjin = D('dyongjin');
        if($isdefault==1){
            return $dyongjin->where("enable",0)->where("isalone",0)->where("type",$type)->find();
        }else {
            return $dyongjin->where("enable",0)->where("isalone",1)->where("user_id",$id)->where("type",$type)->find();
        }
    }
    function getPBaifenbi($type){
        //$isdefault,0不适用默认，1使用默认
        $pyongjin = D('pyongjin');
        return $pyongjin->where("type",$type)->find();
    }
    /**
     * 获取是代理的上级
     * @param unknown $from_id
     */
    function getshangji($from_id){
        $arr = [];
        $tuijian = D('tuijian');
        $res1 = $tuijian->where('from_id',$from_id)->find();
        if ($res1){
            $res2 = $tuijian->where('from_id',$res1['user_id'])->find();
            if ($res2){
                $res3 = $tuijian->where('from_id',$res2['user_id'])->find();
                if ($res3){
                    
                    $u3 = $this->getUserByUserId2($res3['user_id']);
                    if ($u3&&$u3['is_daili']){
                        $dy3 = $this->getDailiBaifenbi($u3['is_usedefault'], 3);
                        $t3['user_id'] = $res3['user_id'];
                        $t3['from_id'] = $from_id;
                        $t3['is_daili'] = 1;
                        $t3['baifenbi'] = $dy3['baifenbi'];
                        $t3['dengji'] = 3;
                        array_push($arr, $t3);
                    }
                    
                }
                $u2 = $this->getUserByUserId2($res2['user_id']);
                if ($u2&&$u2['is_daili']){
                    $dy2 = $this->getDailiBaifenbi($u2['is_usedefault'], 2);
                    $t2['user_id'] = $res2['user_id'];
                    $t2['from_id'] = $from_id;
                    $t2['is_daili'] = 1;
                    $t2['baifenbi'] = $dy2['baifenbi'];
                    $t2['dengji'] = 2;
                    array_push($arr, $t2);
                }
            }
            $u1 = $this->getUserByUserId2($res1['user_id']);
            if ($u1&&$u1['is_daili']){
                $dy1 = $this->getDailiBaifenbi($u1['is_usedefault'], 1);
                $t1['user_id'] = $res1['user_id'];
                $t1['from_id'] = $from_id;
                $t1['is_daili'] = 1;
                $t1['baifenbi'] = $dy1['baifenbi'];
                $t1['dengji'] = 1;
                array_push($arr, $t1);
            }
        }
        
        return $arr;
    }

    /**
     * 获取不是代理的上级
     * @param unknown $from_id
     */
    function getshangji2($from_id){
        $arr = [];
        $tuijian = D('tuijian');
        $res1 = $tuijian->where('from_id',$from_id)->find();
        if ($res1){
            $res2 = $tuijian->where('from_id',$res1['user_id'])->find();
            if ($res2){
                $res3 = $tuijian->where('from_id',$res2['user_id'])->find();
                if ($res3){
    
                    $u3 = $this->getUserByUserId2($res3['user_id']);
                    if ($u3&&$u3['is_daili']==0){
                        $dy3 = $this->getPBaifenbi(3);
                        $t3['user_id'] = $res3['user_id'];
                        $t3['from_id'] = $from_id;
                        $t3['is_daili'] = 0;
                        $t3['baifenbi'] = $dy3['baifenbi'];
                        $t3['dengji'] = 3;
                        array_push($arr, $t3);
                    }
    
                }
                $u2 = $this->getUserByUserId2($res2['user_id']);
                if ($u2&&$u2['is_daili']==0){
                    $dy2 = $this->getPBaifenbi(2);
                    $t2['user_id'] = $res2['user_id'];
                    $t2['from_id'] = $from_id;
                    $t2['is_daili'] = 0;
                    $t2['baifenbi'] = $dy2['baifenbi'];
                    $t2['dengji'] = 2;
                    array_push($arr, $t2);
                }
            }
            $u1 = $this->getUserByUserId2($res1['user_id']);
            if ($u1&&$u1['is_daili']==0){
                $dy1 = $this->getPBaifenbi(1);
                $t1['user_id'] = $res1['user_id'];
                $t1['from_id'] = $from_id;
                $t1['is_daili'] = 0;
                $t1['baifenbi'] = $dy1['baifenbi'];
                $t1['dengji'] = 1;
                array_push($arr, $t1);
            }
        }
    
        return $arr;
    }
    function getJiangli(){
        $yongjin = D('jiangli');
        return $yongjin->where("enable",0)->where("is_select",1)->find();
    }
    function getYongjinInfoP(){
        $yongjin = D('pyongjin');
        return $yongjin->select();
    }
    function getYongjinInfoD($isdefault=true,$usr_id=""){
    $yongjin = D('dyongjin');
    if($isdefault){
        return $yongjin->where("enable",0)->where("isalone",0)->select();
    }else{
        return $yongjin->where("enable",0)->where("isalone",1)->where("user_id",$usr_id)->select();
    }
    }
}
